package com.zhkj.shixi.tvplayer.PicturePlayer;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.zhkj.shixi.tvplayer.R;

/**
 * Created by nieshuang on 19-8-9.
 */

public class MyCommonDialog extends Dialog {
//    private final Callback mCallBack;
    private Button mBtnOk;
    private Button mBtnCancel;
    private TextView mTextTitle;
    private TextView mTextContent;
    private String mTitle;
    private String mContent;
    private Context mContext;//上下文
    private int mLayoutResID;//布局文件id
    private int[] mListenedItem;//监听的控件id

    public void setTitleAndContent(String title,String content){
//        mBtnOk = findViewById(R.id.btn_ok);
//        mBtnCancel = findViewById(R.id.btn_cancel);
        mTextTitle = findViewById(R.id.dialog_title);
        mTextTitle.setText("tancuang");
        mTextContent = findViewById(R.id.dialog_content);
        mTextContent.setText("内容");
    }

    public MyCommonDialog(Context context,int layoutResID,int[] listenedItem,String title,String content){
        super(context,R.style.MyCommonDialog);//加载dialog的样式
        this.mContext = context;
//        this.mCallBack = cb;
        this.mLayoutResID = layoutResID;
        this.mListenedItem = listenedItem;
        this.mTitle = title;
        this.mContent = content;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //提前设置Dialog的一些样式
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);//设置dialog显示居中
        //dialogWindow.setWindowAnimations();设置动画效果
        setContentView(mLayoutResID);

        WindowManager windowManager = ((Activity)mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = getWindow().getAttributes();
//        lp.width = display.getWidth()*4/5;// 设置dialog宽度为屏幕的4/5
        lp.height = (int) (display.getHeight() * 0.4); // 高度设置为屏幕的0.4
        lp.width = (int) (display.getWidth() * 0.33); // 宽度设置为屏幕的0.33
        lp.gravity = Gravity.CENTER;
        //设置透明度
        lp.alpha = 1.0f;
        getWindow().setAttributes(lp);
        setCanceledOnTouchOutside(true);//点击外部Dialog消失

        mTextTitle = findViewById(R.id.dialog_title);
        mTextTitle.setText(mTitle);
        mTextContent = findViewById(R.id.dialog_content);
        mTextContent.setText(mContent);

        Button btnOk = findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(mContext,"clicked",Toast.LENGTH_SHORT).show();
            }
        });
        Button btnCancel = findViewById(R.id.btn_cancel);
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//

//                mCallBack.onCancelBtnClick();
            }
        });

        //遍历控件id添加点击注册
//        for(int id:listenedItem){
//            findViewById(id).setOnClickListener(this);
//        }
    }

//    private OnCenterItemClickListener listener;
//    public interface OnCenterItemClickListener {
//        void OnCenterItemClick(MyCommonDialog dialog, View view);
//    }
//    //添加一个接口，然后添加一个方法
//    public void setOnCenterItemClickListener(OnCenterItemClickListener listener) {
//        this.listener = listener;
//    }
//
//
//    @Override
//    public void onClick(View v) {
//        dismiss();//注意：我在这里加了这句话，表示只要按任何一个控件的id,弹窗都会消失，不管是确定还是取消。
//        listener.OnCenterItemClick(this,v);
//    }

    interface Callback {
        void onCancelBtnClick();
    }
}
